import { http } from "@/utils/http";
import { createFormData } from "@pureadmin/utils";

export type UserResult = {
  success: boolean;
  data: {
    shiroUserId: string;
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    shiroUserId: string;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type UserInfoResult = {
  code: number;
  data: {
    account: string;
    avatar:  string;
    birthday: Date;
    deptid:  string;
    email:  string;
    name:  string;
    phone:  string;
    role:  string;
    roles: Array<string>;
    sex: number;
    status: number;
  };
  msg: string;
  success: boolean;
};
export type LoginLogResult = {
	code: number;
  	data: {
	    logname: string;
	    userid:  string;
	    createtime: Date;
	    succeed:  string;
	    message:  string;
	    ip:  string;
	  };
	  msg: string;
	  success: boolean;
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/account/login", { 
	data,
	headers:{
		 "Content-Type": "application/x-www-form-urlencoded",
	}
	 });
};


/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/account/refreshToken", { data });
};

/** 账户设置-个人信息 */
export const getUserInfo = () => {
	return new Promise((resolve, reject) => {
    	http.request<UserInfoResult>("get", "/account/info").then((res) => {
			resolve({
			    	...res.data, 
				nickname: res.data.name,
				username: res.data.account
				
			});
    	}).catch((e) => {
	    	reject(e);
    	});
		
	});
};
/** 个人安全日志 */
export const getLoginLogs = (userId?: string, page?: string, limit?: string) => {
	return http.request<LoginLogResult>("get", "/loginLog/list", { 
		params : {
			userId
			,page
			,limit
			,sort:"createtime"
			,order:"desc"
		}
 	});
};
/** 账户设置 */
export const updateAccountSettings = (data: object) => {
  return http.request<object>("put", "/account/update", { data });
};

/** 登出 */
export const logout = () => {
  return http.request<object>("post", "/account/logout");
};

/** 修改密码 */
export const changePassword = (data: object ) => {
	
  return http.request<object>("put", "/account/changePwd", { 
		data: createFormData(data) ,
	  	headers:{
			 "Content-Type": "application/x-www-form-urlencoded",
		}
	});
};
